﻿using CSSD.Server.DataModel.RoutineBusinessModel;
using DapperEx;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using DapperEx;
using ExtensionHelper;
using Dapper;
using System.Data;
using ExtensionHelper.Util;

namespace DapperExTest.Json
{
    [TestClass]
    public class TestJson1
    {
        public string conn = "LocalSqlServer1";
        public static IList<TrunAroundJournalModel> list;
        public DbBase CreateDbBase()
        {
            return new DbBase(conn);
        }

        [TestInitialize]
        public void Ini()
        {
            long longCount = 0;
            list = CreateDbBase().Page<TrunAroundJournalModel>(1, 1000, out longCount).ToList();

        }
        [TestMethod]
        public void TestToString()
        {
            for (int i = 0; i < 10; i++)
            {
                int count = 10;

                CodeTimer.InitializeByConcurrent();

                TestTime("CustomJson", count, new Action(() =>
                {
                    string s = blqw.Json.ToJsonString(list);
                    Debug.WriteLine(string.Format("1---{0}", s.Length));
                    var ss = blqw.Json.ToObject<List<TrunAroundJournalModel>>(s);
                    Debug.WriteLine(string.Format("1---{0}", ss.Count));
                }));
                TestTime("NetJson", count, new Action(() =>
                {
                    string s = JsonHelper.GetJsonStr(list);
                    Debug.WriteLine(string.Format("2---{0}", s.Length));

                    var ss = JsonHelper.GetObject<List<TrunAroundJournalModel>>(s);
                    Debug.WriteLine(string.Format("2---{0}", ss.Count));
                }));
            }
        }

        [TestMethod]
        public void TestToDataTable()
        {
            for (int i = 0; i < 10; i++)
            {
                int count = 10;
                DataTable dt = list.ToDataTable<TrunAroundJournalModel>();

                CodeTimer.InitializeByConcurrent();

                TestTime("NetJson", count, new Action(() =>
                {
                    string s = JsonHelper.GetJsonStr(dt);
                    Debug.WriteLine(string.Format("2---{0}", s.Length));

                    var ss = JsonHelper.GetObject<DataTable>(s);
                    Debug.WriteLine(string.Format("2---{0}", ss.Rows.Count));
                }));
            }
        }

        [TestMethod]
        public void TestToAnonymous()
        {
            for (int i = 0; i < 10; i++)
            {
                int count = 10;

                var model = new { TurnAroundID = 10011, DeliveryPointIdActual = "111", CreateTime = DateTime.Now, money = 2.3f, money1 = 2.3M,TurnModel = list };

                CodeTimer.InitializeByConcurrent();

                TestTime("NetJson", count, new Action(() =>
                {
                    string s = JsonHelper.GetJsonStr(model);
                    Debug.WriteLine(string.Format("2---{0}", s.Length));

                    var ss = JsonHelper.GetAnonymousObject(s, model);
                    Debug.WriteLine(string.Format("2---{0}", ss.TurnModel[0].AccountCode));
                }));
            }
        }

        public void TestTime(string name, int count, Action action)
        {
            CodeTimer.Execute(name, count, action, new Action<string>((str) =>
            {
                Debug.WriteLine(string.Format("{0}---{1}", name, str));
            }));
        }
    }
}
